package com.example.baserecyclerviewadapterdemo.adapter.node.section;

import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.entity.node.section.ItemNode;
import com.example.baserecyclerviewadapterdemo.entity.node.section.RootFooterNode;
import com.example.baserecyclerviewadapterdemo.entity.node.section.RootNode;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class NodeSectionAdapter extends BaseNodeAdapter {
    public NodeSectionAdapter() {
        super();
        addFullSpanNodeProvider(new RootNodeProvider());
        addNodeProvider(new SecondNodeProvider());
        addFooterNodeProvider(new RootFooterNodeProvider());
    }

    @Override
    protected int getItemType(@NotNull List<? extends BaseNode> nodes, int position) {
        BaseNode baseNode = nodes.get(position);
        if (baseNode != null) {
            if (baseNode instanceof RootNode) {
                return 0;
            }else if(baseNode instanceof ItemNode){
                return 1;
            }else if(baseNode instanceof RootFooterNode){
                return 2;
            }
        }
        return -1;
    }

    private class RootNodeProvider extends BaseNodeProvider {
        @Override
        public int getItemViewType() {
            return 0;
        }

        @Override
        public int getLayoutId() {
            return R.layout.def_section_head;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
            RootNode rootNode = (RootNode) baseNode;
            holder.setText(R.id.header,rootNode.getTitle());
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            getAdapter().expandOrCollapse(position);
        }
    }

    private class SecondNodeProvider extends BaseNodeProvider {
        @Override
        public int getItemViewType() {
            return 1;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_section_content;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
            if (baseNode == null) {
                return;
            }
            ItemNode itemNode = (ItemNode) baseNode;
            holder.setImageResource(R.id.iv,itemNode.getImg());
            holder.setText(R.id.tv,itemNode.getName());
        }
    }

    private class RootFooterNodeProvider extends BaseNodeProvider {
        public RootFooterNodeProvider() {
            addChildClickViewIds(R.id.footerTv);
        }

        @Override
        public int getItemViewType() {
            return 2;
        }

        @Override
        public int getLayoutId() {
            return R.layout.node_footer;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode baseNode) {
            RootFooterNode footerNode = (RootFooterNode) baseNode;
            holder.setText(R.id.footerTv,footerNode.getTitle());
        }

        @Override
        public void onChildClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            if (view.getId() == R.id.footerTv) {
                Toast.makeText(context, "Click Position:" + position, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
